package jsyf.oa.web.apply;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.socket.TextMessage;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.apply.MoneyFacade;
import jsyf.oa.facade.system.MessageFacade;
import jsyf.oa.web.socket.MyWebSocketHandler;

/**
 * @author GUOQI
 */
@Api(value = "申请事项接口", description = "申请事项接口")
@Controller
@RequestMapping("/apply")
public class MoneyController {
    @Resource
    private MoneyFacade moneyFacade;
    @Resource
    private MessageFacade messageFacade;
    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    @RequestMapping(value = "/saveRefund.json", method = RequestMethod.POST)
    public void saveRefund(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = moneyFacade.saveRefund(jsonData);
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        sendMessage(result.getString("message"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("添加回款信息失败"));
        }
    }

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = moneyFacade.save(jsonData);
        //发送消息
        sendMessage(jsonObject.getString("message"));
        jsonObject.remove("message");
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @ApiOperation(
            value = "获取用款未处理数量",
            notes = "获取用款未处理数量",
            httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "2017121618514C22B68296F31AF8DDE2", dataType = "String", paramType = "query")
    @RequestMapping(value = "/getUnDoCount.json", method = RequestMethod.GET)
    public void getUnDoCount(HttpServletResponse response, @RequestParam String userId) {
        String count = moneyFacade.getUnDoCount(userId);
        JSONObject records = new JSONObject();
        records.put("count", count);
        if (count != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }


    @RequestMapping(value = "/getByPage.json", method = RequestMethod.POST)
    public void getByPage(HttpServletResponse response, @RequestBody String jsonData) {

        JSONObject records = this.moneyFacade.getByPage(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }


    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("id");
        if (StringUtils.isBlank(id)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
            return;
        }
        JSONObject records = this.moneyFacade.getById(id);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        JSONObject result = moneyFacade.applyAudit(jsonObject.getString("id"), jsonObject.getString("userId"), jsonObject.getString("auditFlag"), jsonObject.getString("advise"));
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        sendMessage(result.getString("message"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("用款审核失败"));
        }
    }

    @RequestMapping(value = "/payAudit.json", method = RequestMethod.POST)
    public void payAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        JSONObject result = moneyFacade.payAudit(jsonObject.getString("ids"), jsonObject.getString("userId"), jsonObject.getString("auditFlag"), jsonObject.getString("advise"));
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        sendMessage(result.getString("message"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("付款审核失败"));
        }
    }

    @RequestMapping(value = "/submitPayAudit.json", method = RequestMethod.POST)
    public void submitPayAudit(HttpServletResponse response, @RequestBody String jsonData) {
        boolean result = moneyFacade.submitPayAudit(jsonData);
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("提交审核失败"));
        }
    }

    @RequestMapping(value = "/payMoney.json", method = RequestMethod.POST)
    public void payMoney(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        JSONObject result = moneyFacade.payMoney(jsonObject.getString("id"),
                jsonObject.getString("payUserId"),
                jsonObject.getString("payRemark"),
                jsonObject.getString("payUserName"));
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        sendMessage(result.getString("message"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("付款失败"));
        }
    }

    @RequestMapping(value = "/getPayerListByMoneyId.json", method = RequestMethod.POST)
    public void getPayerListByMoneyId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        List<JSONObject> result = moneyFacade.getPayerListByMoneyId(jsonObject.getString("moneyId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void removeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = moneyFacade.removeById(jsonObject.getString("id"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/copierReturn.json", method = RequestMethod.POST)
    public void copierReturn(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = moneyFacade.copierReturn(jsonObject.getString("id"), jsonObject.getString("copierPersonId"), jsonObject.getString("auditFlag"), jsonObject.getString("copierReturnReason"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/payReturn.json", method = RequestMethod.POST)
    public void payReturn(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        String id = jsonObject.getString("id");
        String payReturnUserId = jsonObject.getString("payReturnUserId");
        String payReturnUserName = jsonObject.getString("payReturnUserName");
        String payReturnReasonType = jsonObject.getString("payReturnReasonType");
        String payReturnReasonDesc = jsonObject.getString("payReturnReasonDesc");
        Boolean result = moneyFacade.payReturn(id, payReturnUserId, payReturnUserName, payReturnReasonType, payReturnReasonDesc);
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    private void sendMessage(String jsonData) {
        List<JSONObject> jsonObjectList = null;
        if (!StringUtils.isBlank(jsonData)) {
            jsonObjectList = FastJsonConvert.convertJSONToArray(jsonData, JSONObject.class);
        }
        if (null != jsonObjectList && jsonObjectList.size() > 0) {
            TextMessage textMessage = new TextMessage(BaseUtils.succsessArrHourTimeRespond(jsonObjectList), true);
            try {
                myWebSocketHandler.sendMsgToAllUsers(textMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/getByIdForRefund.json", method = RequestMethod.POST)
    public void getByIdForRefund(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = moneyFacade.getByIdForRefund(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(jsonObject));
    }

    @RequestMapping(value = "/refundAudit.json", method = RequestMethod.POST)
    public void refundAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = moneyFacade.refundAudit(jsonData);
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        sendMessage(result.getString("message"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond("审核成功"));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("提交审核失败"));

        }
    }

    @RequestMapping(value = "/getPayeeList.json", method = RequestMethod.POST)
    public void getPayeeList(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        List<JSONObject> result = moneyFacade.getPayeeList(jsonObject.getString("createUserId"), jsonObject.getString("key"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrTimeRespond(result));
    }
}
